package kr.hkit.powerjava.chapter16;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

class TrafficLightPanel extends JPanel implements ActionListener{

	boolean flag = false;
	private int light_number =0;
	
	/**
	 * Create the panel.
	 */
	public TrafficLightPanel() {
		setLayout(new BorderLayout());
		JButton btn = new JButton("신호등 ON");
		
		btn.addActionListener(this);
		add(btn, BorderLayout.SOUTH);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.black);
		g.drawOval(100, 100, 100, 100);
		g.drawOval(100, 200, 100, 100);
		g.drawOval(100, 300, 100, 100);
		
		if(light_number ==0){
			g.setColor(Color.red);
			g.fillOval(100, 100, 100, 100);
		}else if(light_number ==1){
			g.setColor(Color.GREEN);
			g.fillOval(100, 200, 100, 100);
		}else{
			g.setColor(Color.YELLOW);
			g.fillOval(100, 300, 100, 100);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(++light_number >= 3){
			light_number = 0;
		}
		repaint();
	}
}

public class TrafficLightFrame extends JFrame {

	private TrafficLightPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TrafficLightFrame frame = new TrafficLightFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TrafficLightFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 500);
		contentPane = new TrafficLightPanel();
		
		setContentPane(contentPane);
	}

}
